﻿using AutoMapper.Internal.Mappers;
using Mes.Pendant.ProcurementWarehousings.Dto;
using Mes.Pendant.ProcurementWarehousings;
using Mes.Pendant.SupplierInterface;
using Mes.Pendant.SupplierInterface.SupplierDto;
using Mes.Pendant.SupplierManagements;
using Mes.Pendant.UnitMeasures.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Mes.Pendant.Common.DtoMapping;

namespace Mes.Pendant.SupplierServices
{
	public class SupplierService : ApplicationService, ISupplierService
    {
        private readonly IRepository<SupplierManagement, int> _repository;

        public SupplierService(IRepository<SupplierManagement, int> repository)
        {
            _repository = repository;
        }

        public async Task<int> AddSupplier(SupplierManagementDto supplierManagementDto)
        {
            var supplierCode = _repository.AnyAsync(m=>m.SupplierCode == supplierManagementDto.SupplierCode).Result;
            if(supplierCode)
            {
                return -1;
            }

            var supplierName = _repository.AnyAsync(m => m.SupplierName == supplierManagementDto.SupplierName).Result;
            if (supplierName)
            {
                return -1;
            }

            var spplierModel = supplierManagementDto.MapTo<SupplierManagement>();
            await _repository.InsertAsync(spplierModel);
            return supplierManagementDto.Id;
        }

        public async Task<PagedResult> GetSupplierrList(string? supplierCode, string? supplierName, string? abbreviation, bool? state, int page = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<SupplierManagement>(true);
            if (!string.IsNullOrEmpty(supplierCode))
            {
                predicate = predicate.And(m => m.SupplierCode.Contains(supplierCode));
            }
            if (!string.IsNullOrEmpty(supplierName))
            {
                predicate = predicate.And(m => m.SupplierName.Contains(supplierName));
            }
            if (!string.IsNullOrEmpty(abbreviation))
            {
                predicate = predicate.And(m => m.SupplierAbbreviation.Contains(abbreviation));
            }
            if (state != null)
            {
                predicate = predicate.And(m => m.State == state);
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<int> DeleteSupplier(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
        }

        public async Task<int> UpdateSupplier(SupplierManagementDto supplierManagementDto)
        {
            var supplierName = await _repository.AnyAsync(m => m.SupplierName == supplierManagementDto.SupplierName && m.Id != supplierManagementDto.Id);

            SupplierManagement spplierManagement = supplierManagementDto.MapTo<SupplierManagement>();
            await _repository.UpdateAsync(spplierManagement);
            return spplierManagement.Id;
        }

        public async Task<SupplierManagementDto> GetSupplierInfo(int id)
         {
            var entity = _repository.GetAsync(id).Result;
            var supplierManagementDto = ObjectMapper.Map<SupplierManagement, SupplierManagementDto>(entity);
            return supplierManagementDto;
        }
    }
}
